package com.iplant.wms.serviceImpl;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.WPOService;
import com.iplant.wms.service.po.wpo.WMSPurchaseOrder;
import com.iplant.wms.service.po.wpo.WMSPurchaseOrderItem;
import com.iplant.wms.serviceImpl.dao.wpo.WMSPurchaseOrderDAO;
import com.iplant.wms.serviceImpl.dao.wpo.WMSPurchaseOrderItemDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WPOServiceImpl implements WPOService {

    private static Logger logger = LoggerFactory.getLogger(WPOServiceImpl.class);

    public WPOServiceImpl() {
    }

    private static WPOService Instance;

    public static WPOService getInstance() {
        if (Instance == null) {
            Instance = new WPOServiceImpl();
        }
        return Instance;
    }

    @Override
    public ServiceResult<Integer> WMS_UpdatePurchaseOrder(BMSEmployee wLoginUser,
                                                          WMSPurchaseOrder wPurchaseOrder) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wPurchaseOrder.setUserInfo(wLoginUser);
            wResult.Result =
                    WMSPurchaseOrderDAO.getInstance().UpdatePo(wLoginUser, wPurchaseOrder, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<Integer> WMS_DeletePurchaseOrderList(BMSEmployee wLoginUser,
                                                              List<WMSPurchaseOrder> wPurchaseOrderList) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);


            WMSPurchaseOrderDAO.getInstance().DeletePOList(wLoginUser, wPurchaseOrderList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<Integer> WMS_UpdatePurchaseOrderItemList(BMSEmployee wLoginUser,
                                                                  List<WMSPurchaseOrderItem> wWMSPurchaseOrderItemList) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            WMSPurchaseOrderItemDAO.getInstance().UpdatePoList(wLoginUser, wWMSPurchaseOrderItemList, wErrorCode);


        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<Integer> WMS_DeletePurchaseOrderItemList(BMSEmployee wLoginUser,
                                                                  List<WMSPurchaseOrderItem> wPurchaseOrderItemList) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);


            WMSPurchaseOrderItemDAO.getInstance()
                    .DeletePOList(wLoginUser, wPurchaseOrderItemList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<Integer> WMS_ImportPurchaseOrder(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            // ①采购订单
            List<WMSPurchaseOrder> wWMSPurchaseOrderList = WMSPurchaseOrderDAO.getInstance()
                    .GetWMSPurchaseOrderList(wLoginUser, wExcelData, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
                return wResult;
            }


            Map<String, Integer> wOrderNoMap = new HashMap<>();
            for (WMSPurchaseOrder wWMSPurchaseOrder : wWMSPurchaseOrderList) {
                if (StringUtils.isEmpty(wWMSPurchaseOrder.Code)) {
                    continue;
                }
                WMSPurchaseOrderDAO.getInstance().UpdatePo(wLoginUser, wWMSPurchaseOrder, wErrorCode);

                if (wErrorCode.Result != 0 || wWMSPurchaseOrder.ID <= 0) {
                    continue;
                }
                wOrderNoMap.put(wWMSPurchaseOrder.Code, wWMSPurchaseOrder.ID);
                WMSPurchaseOrderItemDAO.getInstance()
                        .WMS_DeletePurchaseOrderItemList(wLoginUser, wWMSPurchaseOrder.ID, wErrorCode);

                if (wErrorCode.Result != 0) {
                    continue;
                }

            }

            // ②采购订单明细
            List<WMSPurchaseOrderItem> wWMSPurchaseOrderItemList = WMSPurchaseOrderItemDAO.getInstance()
                    .GetWMSPurchaseOrderItemList(wLoginUser, wExcelData);
            for (WMSPurchaseOrderItem wWMSPurchaseOrderItem : wWMSPurchaseOrderItemList) {
                if (StringUtils.isEmpty(wWMSPurchaseOrderItem.PurchaseOrderCode) || wOrderNoMap.containsKey(
                        wWMSPurchaseOrderItem.PurchaseOrderCode)) {
                    continue;
                }
                wWMSPurchaseOrderItem.PurchaseOrderID = wOrderNoMap.get(wWMSPurchaseOrderItem.PurchaseOrderCode);

            }
            WMSPurchaseOrderItemDAO.getInstance()
                    .UpdatePoList(wLoginUser, wWMSPurchaseOrderItemList, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<WMSPurchaseOrder> WMS_QueryPurchaseOrder(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<WMSPurchaseOrder> wResult = new ServiceResult<WMSPurchaseOrder>();
        wResult.Result = new WMSPurchaseOrder();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            wResult.Result =
                    WMSPurchaseOrderDAO.getInstance().SelectPO(wLoginUser, wID, wCode, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<WMSPurchaseOrderItem> WMS_QueryPurchaseOrderItem(BMSEmployee wLoginUser, int wID,
                                                                          String wCode) {
        ServiceResult<WMSPurchaseOrderItem> wResult = new ServiceResult<>();
        wResult.Result = new WMSPurchaseOrderItem();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            wResult.Result =
                    WMSPurchaseOrderItemDAO.getInstance()
                            .SelectPO(wLoginUser, wID, wCode, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


}
